Public Class frmSummaryAbnormalReport


    Delegate Sub delGenerateReport()

    Private Sub frmSummaryAbnormalReport_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        loadYear()
        loadOrganization("rbnReglion", "10000", False)
    End Sub

    Private Sub loadYear()
        Dim sumYear As New PoliceHealthClass.ManageReportControl()
        Dim yearDS As New System.Data.DataSet()
        yearDS = sumYear.getSummaryAbnormalReportYear()
        With cbbYear
            .DataSource = yearDS.Tables("SummaryReportYear")
            .DisplayMember = "year"
            .ValueMember = "year"
        End With
    End Sub

    Private Sub loadProvince()
        Dim sumProvince As New PoliceHealthClass.ManageReportControl()
        Dim provinceDS As New System.Data.DataSet()
        provinceDS = sumProvince.getProvince()
        With cbbProvince
            .DataSource = provinceDS.Tables(0)
            .DisplayMember = "province_name_tha"
            .ValueMember = "province_id"
        End With
    End Sub

    Private Sub loadOrganization(ByVal inRBNName As String, ByVal inOrganizationID As String, ByVal inStatus As Boolean)
        Dim sumOrganization As New PoliceHealthClass.ManageReportControl()
        Dim organizationDS As New System.Data.DataSet()
        Select Case inRBNName
            Case "rbnReglion"
                Select Case inOrganizationID
                    Case "10000"
                        organizationDS = sumOrganization.getSummaryReportOrganization(inOrganizationID, inRBNName)
                        With cbbProvince
                            .DataSource = organizationDS.Tables("SummaryReportOrganization")
                            .DisplayMember = "organization_name_tha"
                            .ValueMember = "organization_id"
                        End With
                        With cbbOrganization
                            .DataSource = organizationDS.Tables("SummaryReportOrganization")
                            .DisplayMember = "organization_name_tha"
                            .ValueMember = "organization_id"
                        End With
                    Case "98000"
                        organizationDS = sumOrganization.getSummaryReportOrganization(inOrganizationID, inRBNName)
                        With cbbOrganization
                            .DataSource = organizationDS.Tables("SummaryReportOrganization")
                            .DisplayMember = "organization_name_tha"
                            .ValueMember = "organization_id"
                        End With
                    Case "99000"
                        organizationDS = sumOrganization.getSummaryReportOrganization(inOrganizationID, inRBNName)
                        With cbbOrganization
                            .DataSource = organizationDS.Tables("SummaryReportOrganization")
                            .DisplayMember = "organization_name_tha"
                            .ValueMember = "organization_id"
                        End With
                    Case Else
                        organizationDS = sumOrganization.getSummaryReportOrganization(inOrganizationID, inRBNName)
                        With cbbOrganization
                            .DataSource = organizationDS.Tables("SummaryReportOrganization")
                            .DisplayMember = "organization_name_tha"
                            .ValueMember = "organization_id"
                        End With
                End Select
            Case "rbnProvince"
                Select Case inOrganizationID
                    Case "00000"
                        organizationDS = sumOrganization.getSummaryReportOrganization(inOrganizationID, inRBNName)
                        With cbbProvince
                            .DataSource = organizationDS.Tables("SummaryReportOrganization")
                            .DisplayMember = "organization_name_tha"
                            .ValueMember = "organization_id"
                        End With
                    Case Else
                        organizationDS = sumOrganization.getSummaryReportOrganization(inOrganizationID, inRBNName)
                        With cbbOrganization
                            .DataSource = organizationDS.Tables("SummaryReportOrganization")
                            .DisplayMember = "organization_name_tha"
                            .ValueMember = "organization_id"
                        End With
                End Select
            Case "rbnAmphur"
                organizationDS = sumOrganization.getSummaryReportOrganization(inOrganizationID, inRBNName)
                With cbbOrganization
                    .DataSource = organizationDS.Tables("SummaryReportOrganization")
                    .DisplayMember = "organization_name_tha"
                    .ValueMember = "organization_id"
                End With
        End Select
    End Sub

    Private Sub rbnReglion_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbnReglion.CheckedChanged
        cbbProvince.Enabled = True
        cbbOrganization.Enabled = False
        loadOrganization("rbnReglion", "10000", False)
    End Sub

    Private Sub rbnProvince_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbnProvince.CheckedChanged
        cbbProvince.Enabled = True
        cbbOrganization.Enabled = False
        loadOrganization("rbnProvince", "00000", False)
        loadOrganization("rbnProvince", "50000", False)
    End Sub

    Private Sub rbnAmphur_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbnAmphur.CheckedChanged
        loadProvince()
        cbbProvince.Enabled = True
        cbbOrganization.Enabled = True
        loadOrganization("rbnAmphur", cbbProvince.SelectedValue & "000", False)
    End Sub

    Private Sub GenerateReport()
        Dim sumReport As New PoliceHealthClass.ManageReportControl()
        Dim sumDS As New System.Data.DataSet()
        sumDS = sumReport.getSummaryAbnormalReport(cbbYear.SelectedValue, cbbOrganization.SelectedValue)
        Dim currentReport As New SummmaryAbnormalReport()

        currentReport.SetDataSource(sumDS.Tables("SummaryAbnormalReport"))
        'currentReport.SetParameterValue("org_main", _
        '    sumReport.getSummaryReportOrganizationParentName(cbbOrganization.SelectedValue))
        'currentReport.SetParameterValue("start_date", dtpStartDate.Value)
        'currentReport.SetParameterValue("end_date", dtpEndDate.Value)
        'currentReport.SetParameterValue("police_all", _
        '    sumReport.getSummaryReportPoliceInOrganization(cbbOrganization.SelectedValue))
        'currentReport.SetParameterValue("prename", cbbPrename.SelectedValue)
        'currentReport.SetParameterValue("full_name", txtFullname.Text)
        'currentReport.SetParameterValue("position_name", txtPosition.Text)
        'currentReport.SetParameterValue("org_sub", _
        '    sumReport.getSummaryReportOrganizationName(cbbOrganization.SelectedValue))

        crvSummaryAbnormalReport.ReportSource = currentReport
        crvSummaryAbnormalReport.Zoom(74)
        crvSummaryAbnormalReport.Refresh()
    End Sub

    Private Sub btnShowReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowReport.Click

        ConnectionString.ShowLoadingForm()
        Me.Invoke(New delGenerateReport(AddressOf GenerateReport))
        ConnectionString.TerminateLoadingForm()

        'GenerateReport()
    End Sub

    Private Sub cbbProvince_SelectionChangeCommitted(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbbProvince.SelectionChangeCommitted
        Dim currSelectValue As String = cbbProvince.SelectedValue.ToString()
        If cbbProvince.SelectedValue = "99000" Then
            cbbOrganization.Enabled = True
            loadOrganization("rbnReglion", cbbProvince.SelectedValue, True)
        ElseIf cbbProvince.SelectedValue = "98000" Then
            cbbOrganization.Enabled = True
            loadOrganization("rbnReglion", cbbProvince.SelectedValue, True)
        ElseIf cbbProvince.SelectedValue = "15000" Then
            cbbOrganization.Enabled = False
            loadOrganization("rbnReglion", cbbProvince.SelectedValue, True)
        ElseIf cbbProvince.SelectedValue = "16000" Then
            cbbOrganization.Enabled = False
            loadOrganization("rbnReglion", cbbProvince.SelectedValue, True)
        ElseIf cbbProvince.SelectedValue = "50040" Then
            cbbOrganization.Enabled = True
            loadOrganization("rbnProvince", "50040", False)
        ElseIf cbbProvince.SelectedValue = "65020" Then
            cbbOrganization.Enabled = True
            loadOrganization("rbnProvince", "65020", False)
        ElseIf rbnAmphur.Checked Then
            loadOrganization("rbnAmphur", cbbProvince.SelectedValue & "000", False)
        Else
            cbbOrganization.Enabled = False
            'loadOrganization("rbnProvince", "15000", False)
            loadOrganization("rbnProvince", cbbProvince.SelectedValue, False)
            cbbProvince.SelectedValue = currSelectValue
        End If
    End Sub

End Class